package cn.whdream.datastructure.linkedlist;

import java.util.Stack;

/**
 * 
 * @author admin 链表顺序添加
 */
public class SingleLinkedListDemo2 {
	public static void main(String[] args) {
		SingleLinkedList2 list = new SingleLinkedList2();
		list.addHeroNodeByOrderAsc(new HeroNode("李逵", "黑旋风", 4));
		list.addHeroNodeByOrderAsc(new HeroNode("宋江", "及时雨", 1));
		list.addHeroNodeByOrderAsc(new HeroNode("卢俊义", "玉麒麟", 2));
		list.addHeroNodeByOrderAsc(new HeroNode("吴用", "智多星", 3));
		list.show();
//		list.updateHeroNodeByNo(new HeroNode("诸葛亮", "智多星", 3));
//		list.show();
//		list.deleteHeroNodeByNo(3);
//		list.show();
//		list.showLength();
//		System.out.println(list.findLastIndexNode(4));
	//	System.out.println("翻转列表");
	//	list.showReverseList();
		list.printReverseList();
	}

	static class SingleLinkedList2 {
		private final HeroNode head = new HeroNode("", "", 0);

		public void addHeroNode(HeroNode node) {
			HeroNode temp = head;
			while (true) {
				if (temp.next == null) {
					break;
				}
				temp = temp.next;
			}
			temp.next = node;

		}
		public int showLength() {
			int len = 0;
			if(head.next == null) {
				System.out.println("0");
				return 0;
			}
			HeroNode node = head.next;
			while(true) {
				if (node != null) {
					len++;
				}else {
					break;
				}
				node = node.next;
			}
			System.out.println(len);
			return len;
		}
		/**
		 * 
		 * @param head 头结点
		 * @param index 倒数的第几个
		 * @return
		 */
		public HeroNode findLastIndexNode(int index) {
			int len = showLength();
			if (len == 0 ||index <= 0|| index > len) {
				return null;  
			}
			int findIndex = len - index;
			HeroNode node = head.next;
			int curIndex = -1;
			while(true) {
				if (node != null) {
					curIndex++;
					if(curIndex == findIndex) {
						return node;
					}
				}else {
					break;
				}
				node = node.next;
			}
			return null;
		}
		/**
		 * 升序排序
		 * @param node 结点
		 */
		public void addHeroNodeByOrderAsc(HeroNode node) {
			HeroNode temp = head;
			//用于提示标签
			boolean flag = false;
			while (true) {
				//头结点之后没有数据，直接插入
				if (temp.next == null) {
					temp.next = node;
					break;	
				}
				if (temp.next.no == node.no) {
					flag = true;
					break;
				}else if(temp.next.no > node.no){
					//插入的数字小，则放到前面
					HeroNode currNode = temp.next;
					temp.next = node;
					node.next = currNode;
					break;
				}else {
					temp = temp.next;
				}
			}
			
			if(flag) {
				System.out.println("该排名已经有英雄，无法插入");
			}
		}
		public void updateHeroNodeByNo(HeroNode h) {
			if(head.next == null) {
				System.out.println("链表为空");
				return;
			}
			HeroNode temp = head.next;
			while(true) {
				if(temp == null) {
					break;
				}
				if(temp.no == h.no) {
					temp.name = h.name;
					temp.nickName = h.nickName;
					System.out.println("更新成功:"+temp);
					return;
				}
				temp = temp.next;
			}
			System.out.println("未找到对应英雄");
		}
		public void deleteHeroNodeByNo(int no) {
			HeroNode temp = head;
			while(true) {
				if(temp.next == null) {
					break;
				}
				if(temp.next.no == no) {
					HeroNode nextNodeChild = temp.next.next;
					HeroNode deleteNode = temp.next;
					temp.next = nextNodeChild;
					System.out.println("删除成功"+deleteNode);
					return;
				}
				temp = temp.next;
			}
			System.out.println("未找到对应英雄");
		}
		
		public void show() {
			HeroNode temp = head;
			while (true) {
				if (temp.next == null) {
					break;
				}
				System.out.println(temp.next);
				temp = temp.next;
			}
		}
		public void showReverseList() {
			HeroNode reverseNodeHead = new HeroNode("", "", 0);
			HeroNode temp = head.next;
			if (head.next == null) {
				System.out.println("列表为空");
				return;
			}
			while(true) {
				if(temp == null) {
					break;
				}
				
				//先把当前的 temp保存起来
				HeroNode currentNode = temp;
				//将指针指向下一个
				temp = temp.next;
				//将下一个执行 反转列表的next
				currentNode.next = reverseNodeHead.next;
				//将反转head关联到当前 node上
				reverseNodeHead.next = currentNode;
			}
			head.next = reverseNodeHead.next;
			show();
			
		}
		
		public void printReverseList() {
			if (head.next == null) {
				System.out.println("链表为空");
				return;
			}
			Stack<HeroNode> stack = new Stack<>();
			HeroNode node = head.next;
			while (true) {
				if(node == null) {
					break;
				}
				stack.add(node);
				node = node.next;
			}
			System.out.println("堆栈开始打印反转列表");
			while(!stack.isEmpty()) {
				System.out.println(stack.pop());
			}
			
		}
	}

	static class HeroNode {
		String name;
		String nickName;
		int no;
		HeroNode next;

		public HeroNode(String name, String nickName, int no) {
			super();
			this.name = name;
			this.nickName = nickName;
			this.no = no;
		}

		@Override
		public String toString() {
			return "HeroNode [name=" + name + ", nickName=" + nickName + ", no=" + no + "]";
		}

	}
}
